/**
 * 定义itfreer.form.builder模块
 */
(function(window, angular, undefined){
	/* 
	 * 定义构建模块
	 */
	var itfreerformBuilderModule = angular.module('itfreer.form.builder', ['itfreer.form.core']);

	/**
	 * 定义表单变量
	 */
	itfreerformBuilderModule.value('$builderObject',{
		// 定义当前查询列表
		baseTables : [],
		// 包名称
		jarName : "com.hrt.ps.data",
		// 构建路径
		buildPath : "c:\\builder"
	});

	/**
	 * 定义表单数据访问服务
	 */
	itfreerformBuilderModule.factory('$itfreer_form_builder', 
			function($rootScope, $http, $builderObject, $itfreer_core, $itfreerHttp) {
		$itfreer_form_builder = {
			// 导入字段数据
			inputFields : function(formName, tableName, fieldTableName, keyField){
				formName = formName ? formName : 'fd';
				if(!$formObject[formName].editRow.hasOwnProperty('childValues')){
					$formObject[formName].editRow.childValues = {};
				}
				$itfreerHttp.getTableFields(fieldTableName,
					function(data){
						$rootScope.$apply(function() {
							$formObject[formName].editRow.childValues[tableName] = [];
							var list = [];
							data.forEach(function(e){  
								var obj = {};
								var guid = $itfreer_core.getGUID();
								obj[keyField] = guid;
								obj[$formObject.isNew]=true;
								obj.S_FIELDNAME=e.fieldName;
								obj.S_FIELDALIASNAME=e.fieldAliasName;
								obj.S_DATATYPE=e.dataType;
								obj.I_MAXLEN=e.maxLen;
								obj.I_DECIMALDIGITS=e.decimaldigits;
								obj.S_CONTROLTYPE=e.controlType;
								obj.S_UNIT=e.unit;
								list.push(obj);
								$formObject[formName].editRow.childValues[tableName].push(obj);
							});
							$formObject[formName].editRow.childValues[tableName] = list;
						});
					}, 
					function(ex){
					}
				);
			},
			
			// 构建表单
			builder : function(formID){
				$itfreerHttp.builder(formID, function(data){
					$.messager.alert('系统提示', '构建成功！');
				}, function(ex){
					$.messager.alert('系统提示', '构建失败！');
				});
			},
			
			// 构建数据表
			builderTables : function(){
				$itfreerHttp.builderTables($builderObject.jarName, $builderObject.buildPath, function(data){
					$.messager.alert('系统提示', '构建成功！');
				}, function(ex){
					$.messager.alert('系统提示', '构建失败！');
				});
			},
			
			// 构建数据表
			builderTable : function(id){
				$itfreerHttp.builderTable(id, $builderObject.jarName, $builderObject.buildPath, function(data){
					$.messager.alert('系统提示', '构建成功！');
				}, function(ex){
					$.messager.alert('系统提示', '构建失败！');
				});
			},
			
			// 构建表单
			initBaseTables : function(){
				$itfreerHttp.initBaseTables(function(data){
					$rootScope.$apply(function() {
						$builderObject.baseTables = data;
					});
				}, function(ex){
				});
			}
		};
		for(f in $itfreer_form_builder){
			$rootScope[f] = $itfreer_form_builder[f];
		}
		$rootScope.builderObject = $builderObject;
		return $itfreer_form_builder;
	}); 

	/* 
	 * 加载服务器
	 */
	itfreerformBuilderModule.run(function($rootScope, $itfreer_form_builder, $builderObject) {
	  	
	});
}(window, angular));